package com.ddone.mode.pubsub;

import com.ddone.util.RabbitMqConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author ddone
 * @date 2024/4/4-19:50
 */
public class FanoutConsumer2 {
    private final static String EXCHANGE_NAME = "fanout_exchange";
    private final static String QUEUE_NAME = "fanout_queue2";

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = RabbitMqConnectionUtil.getConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");

        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body);
                System.out.println("[ 消费者2 ] consumer: " + msg + " !");
            }
        };

        channel.basicConsume(QUEUE_NAME, true, consumer);


    }
}
